const fs = require("fs");
const path = require("path");
const { networkInterfaces } = require("os");

// 获取ip地址
exports.getIPAddress = () => {
  const interfaces = networkInterfaces();
  for (let dev in interfaces) {
    let iface = interfaces[dev];
    for (let i = 0; i < iface.length; i++) {
      let { family, address, internal } = iface[i];
      if (family === "IPv4" && address !== "127.0.0.1" && !internal) {
        return address;
      }
    }
  }
};

// 读取文件
exports.getFile = (json_path) => {
  return fs.readFileSync(path.join(__dirname, json_path), "utf8");
};

// 读取文件
exports.setFile = (json_path, data) => {
  return fs.writeFileSync(path.join(__dirname, json_path), data, "utf8");
};
